#include "stdafx.h"
#include "AchievePoint.h"
#include "InstanceTouch.h"
#include "AchieveSeries.h"
#include <algorithm>
DECL_INSTANCE_TOUCH(ETouch_AchievePoint);

struct  AchievePointDefReflectionRegister
{
	AchievePointDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_iCounter,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_AchiveCounter);
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_iTargetNum,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_iRewardCash,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_iSeries,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_AchieveSeries);
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_iOrder,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(AchievePoint,m_sDesc,std::string("default_desc")).Editor_SetAsRaw();

		GlobalClassManager::Instance().RegisterClass(AchievePoint::s_GetReflection(),eDefaultDef_AchievePoint);
		GlobalDefManager::Instance().RegisterDefManger(&AchievePointManager::Instance());

	}
}___AchievePointDefReflectionRegister;

rewrite  void AchievePoint::OnArchieveReaded()
{
	m_pSeries=AchieveSeriesManager::Instance().LookupItem(m_iSeries);
}
static bool __CompareAcharviePointByOrder( const AchievePoint* pA,const AchievePoint* pB)
{
	return pA->m_iOrder<pB->m_iOrder;
}
void AchievePointManager::OnArchieveReaded()
{
	__AchievePointManager::OnArchieveReaded();

	m_mCounterGoup.clear();
	m_mSeriesGoup.clear();
	std::vector<const AchievePoint*> vEnum;
	this->Enumerate(vEnum);
	for(uint32 ii=0;ii<vEnum.size();ii++)
	{
		const AchievePoint* pPoint=vEnum[ii];
		m_mCounterGoup[pPoint->m_iCounter].push_back(pPoint);
		m_mSeriesGoup[pPoint->m_iSeries].push_back(pPoint);
	}
	std::map<int32,std::vector<const AchievePoint*>>::iterator it=m_mSeriesGoup.begin();
	for(;it!=m_mSeriesGoup.end();it++)
	{
		std::vector<const AchievePoint*>& rArray=it->second;
		std::sort(rArray.begin(),rArray.end(),__CompareAcharviePointByOrder);
	}

}

const std::vector<const AchievePoint*>& AchievePointManager::GetGroupOfCounterID( int32 iCounterID )
{
	std::map<int32,std::vector<const AchievePoint*>>::iterator it=m_mCounterGoup.find(iCounterID);
	if(it!=m_mCounterGoup.end())
	{
		return it->second;
	}
	else
	{
		static std::vector<const AchievePoint*> s_dummy;
		return s_dummy;
	}
}
const std::vector<const AchievePoint*>& AchievePointManager::GetGroupOfSeriesID( int32 iSeries )
{
	std::map<int32,std::vector<const AchievePoint*>>::iterator it=m_mSeriesGoup.find(iSeries);
	if(it!=m_mSeriesGoup.end())
	{
		return it->second;
	}
	else
	{
		static std::vector<const AchievePoint*> s_dummy;
		return s_dummy;
	}
}
